home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / qrt.lzh / RELPOS.C < prev    next >
C/C++ Source or Header  |  1989-02-16  |  3KB  |  118 lines

  1.  
  2. /**********************************************************
  3.  
  4.   Relative position module.  This is used for patterns:
  5.   when an object intersection is found, its location is
  6.   passed to one of these routines, which returns two
  7.   object-relative coordinates.
  8.  
  9.  **********************************************************/
  10.  
  11.  
  12. #include "qrt.h"
  13.  
  14. /* #define RELPOSDEBUG 1 */
  15.  
  16. /**********************************************************
  17.  
  18.    Finds relative coords on plane given position in space.
  19.    object should be parallelogram or ring.
  20.    loc is point in space.
  21.    pos1, pos2 are set to relative coords.
  22.  
  23.  **********************************************************/
  24.  
  25. Plane_Pos(obj,loc,pos1,pos2)
  26.   OBJ_PTR obj;
  27.   VECT_PTR loc;
  28.   float *pos1, *pos2;
  29. {
  30.     VECTOR delta;
  31.     register float len1, len2;
  32.  
  33. #   ifdef ROBUST
  34.       if (!((obj->type == RING) ||
  35.             (obj->type == PARALLELOGRAM) ||
  36.             (obj->type == RING) ||
  37.             (obj->type == TRIANGLE)))
  38.         Error(INTERNAL_ERROR,701);
  39. #   endif
  40.  
  41.     VecSubtract(&delta,loc,&(obj->loc));
  42.  
  43.     len1 = sqrt(DotProd(obj->vect1,obj->vect1));
  44.     len2 = sqrt(DotProd(obj->vect2,obj->vect2));
  45.  
  46.     *pos1 = DotProd(delta,obj->vect1)/len1;
  47.     *pos2 = DotProd(delta,obj->vect2)/len2;
  48.  
  49. #   ifdef RELPOSDEBUG
  50.       printf("PLANEPOS: len1,2 = %f %f\n",len1,len2);
  51.       printf("          pos1,2 = %f %f\n",*pos1,*pos2);
  52. #   endif
  53.  
  54. }
  55.  
  56.  
  57. /**********************************************************
  58.  
  59.    Finds relative coords on sphere given position in space
  60.      obj->vect1.x = radius of sphere
  61.  
  62.  **********************************************************/
  63.  
  64. Sphere_Pos(obj,loc,pos1,pos2)
  65.   OBJ_PTR obj;
  66.   VECT_PTR loc;
  67.   float *pos1, *pos2;
  68. {
  69.     float atan2w();
  70.     VECTOR delta;
  71.  
  72. #   ifdef ROBUST
  73.       if (obj->type!=SPHERE) Error(INTERNAL_ERROR,702);
  74. #   endif
  75.  
  76.     VecSubtract(&delta,loc,&(obj->loc));
  77.  
  78. #   ifdef ROBUST
  79.       if (delta.x==0 && delta.y==0 && delta.z==0)
  80.         Error(INTERNAL_ERROR,703);
  81. #   endif
  82.  
  83.     *pos1 = atan2w(delta.x,delta.y) * obj->vect1.x;
  84.     *pos2 = atan2w(sqrt(sqr(delta.x)+sqr(delta.y)),delta.z) *
  85.             obj->vect1.x;
  86.  
  87. #   ifdef RELPOSDEBUG
  88.       printf("SPHEREPOS: pos1,2 = %f %f\n",*pos1,*pos2);
  89. #   endif
  90.  
  91. }
  92.  
  93.  
  94. /**********************************************************
  95.  
  96.  Finds relative coords on quadratic given position in space
  97.  
  98.  **********************************************************/
  99.  
  100. Quadratic_Pos(obj,loc,pos1,pos2)
  101.   OBJ_PTR obj;
  102.   VECT_PTR loc;
  103.   float *pos1, *pos2;
  104. {
  105.    VECTOR newpos;
  106.  
  107.    VecSubtract(&newpos,loc,&(obj->loc));
  108.  
  109.    *pos1 = newpos.x;                     /** This isn't right! **/
  110.    *pos2 = newpos.y;                     /** fix it later      **/
  111.  
  112. #   ifdef RELPOSDEBUG
  113.       printf("QUADRATICPOS: pos1,2 = %f %f\n",*pos1,*pos2);
  114. #   endif
  115. }
  116.  
  117.  
  118.